home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8004 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: ix.netcom.com!netnews
  2. From: "Paul D. DeRocco" <pderocco@ix.netcom.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Exceptions and operator new. Help
  5. Date: Sat, 17 Feb 1996 04:30:46 -0500
  6. Organization: Netcom
  7. Message-ID: <3125A046.202E@ix.netcom.com>
  8. References: <4fu14a$2vg@news.tamu.edu>
  9. NNTP-Posting-Host: ix-bst-ma1-19.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-NETCOM-Date: Sat Feb 17  1:37:12 AM PST 1996
  14. X-Mailer: Mozilla 2.0b6a (Win95; I)
  15.  
  16. You don't have to do that. Allocation and construction are treated as a 
  17. unit by the compiler. If operator new throws an exception, nothing will 
  18. happen. If the constructor throws an exception, operator delete will 
  19. automatically be called before the exception is propagated. If there are 
  20. class members that have destructors and you throw an exception after 
  21. some of them have been successfully constructed, the compiler will 
  22. ensure that the constructed ones will be destroyed and the unconstructed 
  23. ones won't. And if you're dealing with derived classes, an exception 
  24. that is thrown by a derived class constructor will call the destructors 
  25. for all the base and less-derived classes.
  26.  
  27. In other words, it works the way it "should."
  28.  
  29. By the way, if you don't need to handle xalloc locally, but m_pVarList 
  30. is a global that needs to be nullified in case the allocation fails, 
  31. simply nullify it first:
  32.  
  33.     m_pVarList = NULL;
  34.     m_pVarList = new char[10];
  35.  
  36. If the allocation throws an exception, it will never return, so the 
  37. second store into m_pVarList won't happen.
  38.  
  39. -- 
  40.  
  41. Ciao,
  42. Paul D. DeRocco
  43.